package com.hs.pre.module.sort;


import java.io.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hs.common.JSONHandle;
import com.hs.pre.model.SortBean;
import com.hs.pre.page.PageParam;
import com.hs.pre.page.PageUtil;

public class SortAction extends org.apache.struts.actions.DispatchAction{


	private String CODEING = "UTF-8";
	/*****
	 * 分类列表
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id = request.getParameter("id");
		 try {
			 response.setCharacterEncoding(CODEING);
			 PrintWriter out = response.getWriter();
			 SortDAO sortDAO = new SortDAO();
			 List list = sortDAO.getSortTreeList(new Integer(id));

			 /***********/
			 JSONArray jsonArray = JSONArray.fromObject(list);
			 //System.out.println(jsonArray.toString());
			 out.println(jsonArray.toString());
			 out.flush();
			 out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 添加分类
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward addSort(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		String sortName = request.getParameter("sortName");
		String id = request.getParameter("id");
		 try {
			 response.setCharacterEncoding(CODEING);
			 PrintWriter out = response.getWriter();
			 JSONHandle jSONHandle= new JSONHandle();
			 if(sortName == null || (sortName=sortName.trim()).equals("") && id == null || (id = id.trim()).equals(""))
			 {
				
				 out.println(jSONHandle.jsonFailure("分类名为空添加失败!"));
			 }
			 {
				 SortDAO sortDAO = new SortDAO();
				 SortBean sortBean = new SortBean();
				 sortBean.setSortName(sortName);
				 sortBean.setParentSortId(new Integer(id));
				 sortBean = sortDAO.addSort(sortBean);
				 String d = jSONHandle.jsonSuccessObj(sortBean);
				 out.print(d);
			 }
		
			 out.flush();
			 out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 编辑分类
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward editSort(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String sortName = request.getParameter("sortName");
		String id = request.getParameter("id");
		 try {
			 response.setCharacterEncoding(CODEING);
			 PrintWriter out = response.getWriter();
			 JSONHandle jSONHandle= new JSONHandle();
			 if(sortName == null || (sortName=sortName.trim()).equals("") && id == null || (id = id.trim()).equals(""))
			 {
				
				 out.println(jSONHandle.jsonFailure("分类名为空添加失败!"));
			 }
			 {
				 SortDAO sortDAO = new SortDAO();
				 SortBean sortBean = new SortBean();
				 sortBean.setSortName(sortName);
				 sortBean.setSortId(new Integer(id));
				 sortBean = sortDAO.editSort(sortBean);
				 out.print("");
			 }
		
			 out.flush();
			 out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**]
	 *  删除分类
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward delSort(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		 try {
			 
			 PrintWriter out = response.getWriter();
			 SortDAO sortDAO = new SortDAO();
			 sortDAO.delSort(new Integer(id));
			 out.print("");
			 out.flush();
			 out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public ActionForward moresort(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		 PageParam pageParam = new PageParam(request);
		 pageParam.setBasePath("/pre/sortAc.do?sortDis=moresort");
		 /**
		  * 设置 显示个数
		  */
		 pageParam.setPageSize(100);
		 
			 String itemtype = request.getParameter("itemtype");
			 String sortPId = request.getParameter("sortPId");
			 if(itemtype != null && sortPId != null){
				 pageParam.getParamMap().put("itemtype", itemtype);
				 pageParam.getParamMap().put("sortPId", sortPId);
				 PageUtil pageUtil = new SortDAO().getSortSearchChi(Integer.parseInt(sortPId.trim()), pageParam);
				 request.setAttribute("pageUtil", pageUtil);
			 }else 
				 return null;
		 
		return mapping.findForward("moresort");
	}
	
	
	/***
	 * 导入分类
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward excelImpleSort(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			System.out.println("------数据导入--");
			String pdiTem = request.getParameter("pdi");
			try {
				if(pdiTem != null &&! (pdiTem=pdiTem.trim()).equals(""))
				{
				
					// Create a factory for disk-based file items
					FileItemFactory factory = new DiskFileItemFactory();
	
					// Create a new file upload handler
					ServletFileUpload upload = new ServletFileUpload(factory);
					List /* FileItem */ items = upload.parseRequest(request);
					
					Iterator iter = items.iterator();
					FileItem item = null;
					while (iter.hasNext()) {
					    FileItem itemTem = (FileItem) iter.next();
	
					    if (itemTem.isFormField()) {
					    	
					    } else {
					    	item = itemTem;
					    }
					}
					if(item != null)
					{
						ExcelImport excelImport = new ExcelImport(item.getInputStream());
						new SortDAO().impleSort(excelImport.getDealData(Integer.parseInt(pdiTem)));
	
					}
					
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}

	/****
	 * 分类更新父id
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward updateSort(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			String sortId = request.getParameter("sortId");
			String parentSortId = request.getParameter("parentSortId");
			
			try {
				
				SortDAO sortDAO = new SortDAO();
				 PrintWriter out = response.getWriter();
				 JSONHandle jSONHandle= new JSONHandle();
				if(sortDAO.updateSort(new Integer(parentSortId), new Integer(sortId)))
					out.print("");
				else
					out.print(jSONHandle.jsonFailure("更新失败!"));
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
}
